package com.zyl.blog.common.exception;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatusCode;

/**
 * @version v1.0
 * @ProjectName: Personal_Blog
 * @Package: com.zyl.common.exception
 * @ClassName: ApiException
 * @Description: WebClient异常处理增强
 * @Author FinalFantasy
 * @Date 2025/3/14-22:54
 */
@Getter
@Setter
public class ApiException extends RuntimeException {
    private final HttpStatusCode statusCode;
    private final String errorBody;

    public ApiException(HttpStatusCode statusCode, String errorBody) {
        super("API Error: " + statusCode);
        this.statusCode = statusCode;
        this.errorBody = errorBody;
    }

    // 添加错误解析方法
    public <T> T parseError(Class<T> errorType) throws JsonProcessingException {
        return new ObjectMapper().readValue(errorBody, errorType);
    }
}
